package com.stm.bi.core.util;


import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.Properties;

/**
 *
 * 文件上传路径
 * @author ChenYingjun
 *
 */
@Component
public class DirPathUtil implements InitializingBean {

	@Value("${uploadPath.windows:}")
	private String windowsUploadPath;//本地window系统文件路径
	@Value("${uploadPath.linux:}")
	private String linuxUploadPath;//本地Linux系统下文件路径
	private static String uploadPath;

	/**
	 * 获取上传附件
	 * 
	 * @return
	 */
	public String getBasePath() {
		return this.uploadPath;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		Properties props = System.getProperties(); // 系统属性
		if (props.getProperty("os.name").toLowerCase().contains("windows")) {
			this.uploadPath = this.windowsUploadPath;
			if (StringUtils.isEmpty(this.uploadPath)) {
				this.uploadPath = "d:/temp";
			}
			File dir = new File(this.uploadPath);
			try {
				if (!dir.exists() || !dir.isDirectory()) {
					dir.mkdirs();
				}
			} catch (Exception e) {
				this.uploadPath = "d:/temp";
				dir = new File(this.uploadPath);
				if (!dir.exists() || !dir.isDirectory()) {
					dir.mkdirs();
				}
			}
		} else {
			this.uploadPath = this.linuxUploadPath;
			File dir = new File(this.uploadPath);
			if (!dir.exists() || !dir.isDirectory()) {
				dir.mkdirs();
			}
		}
	}
}
